//
//  SpecialistController.m
//  HarmonizePub
//
//  Created by rzhy on 16/8/24.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "SpecialistController.h"
#import "Config.h"
#import <UIScrollView+EmptyDataSet.h>
#import "SpecialistCollectionCell.h"
#import "DepDocListController.h"

static NSString * const CellIdentifier = @"Cell";

@interface SpecialistController () <UICollectionViewDelegate, UICollectionViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSArray *_dataArray;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation SpecialistController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemWidth = XHScreenWidth / 3;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.emptyDataSetSource = self;
    self.collectionView.emptyDataSetDelegate = self;
    UINib *nib = [UINib nibWithNibName:[SpecialistCollectionCell registerName] bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    [self getDpList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getDpList {
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    [HTTPSTool POSTRequestWithPath:XHGetDpList cookie:cookie params:nil success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"科室医生-科室列表: %@", JSON)
        _dataArray = JSON[@"data"][@"list"];
        [_collectionView reloadData];
    }];
}

#pragma mark - collectionView delegate/dataSource Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SpecialistCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _dataArray[indexPath.item];
    [cell setCellShowDataWithDataDictionary:dict];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dataDict = _dataArray[indexPath.item];
    DepDocListController *depDocList = [[DepDocListController alloc] init];
    depDocList.title = dataDict[@"ksmc"];
    depDocList.dataDict = dataDict;
    [self.navigationController pushViewController:depDocList animated:YES];
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getDpList];
}

@end
